package com.kedou.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.kedou.sdk.params.bean.VoucherModel;
import com.kedou.sdk.utils.ResUtils;

import java.util.List;

public class VoucherChooseActivity extends AppBaseActivity{


    private ListView moneyGridView;
    private BaseAdapter moneyAdapter;
    private ImageButton backImage;
    private List<VoucherModel> mMoney;
    private float payMoney;
    private TextView backText;

    private int selectIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutIdentifier("hnssn_activity_voucher"));

        moneyGridView = findViewById(getViewIdentifier("hnssn_activity_payment_type_lv"));
        backImage = findViewById(getViewIdentifier("activity_payment_finish"));
        backText = findViewById(getViewIdentifier("back_text"));
        backText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.putExtra("index",-1);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });


        initdata();
    }

    private void initdata(){
        mMoney = getIntent().getParcelableArrayListExtra("models");
        payMoney = getIntent().getFloatExtra("price",0);
        selectIndex = getIntent().getIntExtra("index",-1);
        initAdapter();
    }



    private void initAdapter(){
        moneyAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return mMoney==null?0:mMoney.size();
            }

            @Override
            public Object getItem(int position) {
                return mMoney.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                MoneyViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder =new MoneyViewHolder();
                    final Context context = parent.getContext();
                    LayoutInflater inflater = LayoutInflater.from(context);
                    final int layoutResId= ResUtils.getLayoutIdentifier(context, "hnssn_item_voucher");
                    final int dateId = ResUtils.getViewIdentifier(context, "item_time");
                    final int moneyResId = ResUtils.getViewIdentifier(context, "item_money");
                    final int contentResId = ResUtils.getViewIdentifier(context, "item_content");
                    final int gameResId = ResUtils.getViewIdentifier(context, "item_des");
                    final int checkboxResId = ResUtils.getViewIdentifier(context, "hnssn_item_payment_checkbox");
                    final int bgResId = ResUtils.getViewIdentifier(context, "item_bg");
                    convertView = inflater.inflate(layoutResId, parent, false);
                    viewHolder.date = convertView.findViewById(dateId);
                    viewHolder.money = convertView.findViewById(moneyResId);
                    viewHolder.bgLayout = convertView.findViewById(bgResId);
                    viewHolder.content = convertView.findViewById(contentResId);
                    viewHolder.gameName = convertView.findViewById(gameResId);
                    viewHolder.checkBox = convertView.findViewById(checkboxResId);
                    convertView.setTag(viewHolder);
                }else {
                    viewHolder = (MoneyViewHolder) convertView.getTag();
                }
                VoucherModel detal = mMoney.get(position);
                viewHolder.money.setText("￥"+((int)detal.getFaceValue()));
                viewHolder.content.setText(detal.getContent());
                viewHolder.date.setText(detal.getUsageEndTime()+"到期");
                viewHolder.gameName.setText("仅限"+detal.getGameName()+"使用");
                viewHolder.checkBox.setChecked(position==selectIndex);
                if(detal.getStatus()!=0||(detal.getThreshold()!=0&&payMoney<detal.getMiCharge())){
                    viewHolder.bgLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(VoucherChooseActivity.this,"hn669_voucher_bg_disenable"));
                    viewHolder.bgLayout.setAlpha(0.3f);
                }else{
                    viewHolder.bgLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(VoucherChooseActivity.this,"hn669_voucher_bg_enable"));
                    viewHolder.bgLayout.setAlpha(1);
                }


                return convertView;
            }
        };
        moneyGridView.setAdapter(moneyAdapter);
        moneyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                seletPosition=position;
//                moneyAdapter.notifyDataSetChanged();
                VoucherModel detal = mMoney.get(position);
                if(detal.getStatus()==0&&(detal.getThreshold()==0||payMoney>=detal.getMiCharge())){
                    Intent intent =new Intent();
                    intent.putExtra("index",position);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        });




    }

    public static class MoneyViewHolder {
        public TextView date;
        public TextView content;
        public TextView money;
        public TextView gameName;
        public LinearLayout bgLayout;
        public CheckBox checkBox;

    }

}
